package com.chilly.io;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Chilly Cui on 2020/9/24.
 */
public class OutputStreamDemo {

    //利用文件节点流实现文件的复制
    @Test
    void FileCopyByStrem() throws IOException {
        File file = new File("c:/env/三国/司马懿.txt");
        FileInputStream fis = new FileInputStream(file);

        FileOutputStream fos = new FileOutputStream("c:/env/三国/司马懿copy.txt");

        int len;
        byte[] data = new byte[1024];
        while ((len = fis.read(data)) != -1) {
            //System.out.println(new String(data, 0, len));
            fos.write(data);
        }

        fis.close();
        fos.close();
    }


    @Test
    void FileOutputStreamTest1() throws IOException {
        File file = new File("c:/env/三国/司马懿.txt");
//        FileOutputStream fos = new FileOutputStream("c:/env/司马懿.txt");
        FileOutputStream fos = new FileOutputStream(file);

        fos.write(97);
        fos.write(98);
        fos.write(99);
        // 文件内容为 abc

        FileOutputStream fos2 = new FileOutputStream("c:/env/三国/赵云.txt", true);
        fos2.write("三国赵云".getBytes());
        // 文件内容为 三国赵云
        fos2.write("有万夫不当之勇".getBytes());

        fos.close();
        fos2.close();
    }
}
